//
//  BBBaseViewController.swift
//  BusinessBao
//
//  Created by Chen on 2017/7/16.
//  Copyright © 2017年 Chen. All rights reserved.
//

import UIKit
import RxSwift
import RxCocoa

class BBBaseViewController: UIViewController {
    
    deinit {
        print("\(NSStringFromClass(self.classForCoder)) \(self) -- dealloc")
    }

    override func viewDidLoad() {
        super.viewDidLoad()

        self.view.backgroundColor = App_BG_Color
        
        self.view.tintColor = Nav_BG_Color
        
        //解决scrolView的content向下偏移20px的问题
//        self.automaticallyAdjustsScrollViewInsets = false
        
        self.setupUI()
    }
    
    /// 状态栏样式
    override var preferredStatusBarStyle: UIStatusBarStyle {
        return .lightContent
    }
    
    func setupUI() {
        
    }
    
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        self.view.endEditing(true)
    }
    
    func showBackButton(touchEventBlock: (() -> Void)?) {
        let backBtn = UIButton(type: .system)
        backBtn.frame = CGRect(x: 0, y: 0, width: 36, height: 36)
        backBtn.setImage(UIImage(named: ""), for: .normal)
        
        weak var weakSelf = self
        backBtn.touchUpInsideCallback {
            if touchEventBlock == nil {
                weakSelf?.navigationController?.popViewController(animated: true)
            } else {
                touchEventBlock?()
            }
        }
        backBtn.rx.tap.subscribe { (event) in
            if touchEventBlock == nil {
                weakSelf?.navigationController?.popViewController(animated: true)
            } else {
                touchEventBlock?()
            }
        }.addDisposableTo(bag)
        
        let backBarItem = UIBarButtonItem(customView: backBtn)
        self.navigationItem.leftBarButtonItem = backBarItem
    }
    
    func showLeftButton(imageName: String?, action: (() -> Void)?) {
        var leftBtn = UIButton(type: .system)
        
        leftBtn = UIButton(frame: CGRect(x: 0, y: 0, width: 36, height: 36))
        leftBtn.setImage(UIImage(named: imageName!), for: .normal)
        leftBtn.isExclusiveTouch = true
        leftBtn.contentHorizontalAlignment = .left

        leftBtn.touchUpInsideCallback {
            action?()
        }
        
        let leftBarItem = UIBarButtonItem(customView: leftBtn)
        self.navigationItem.leftBarButtonItem = leftBarItem
    }
    
    func showRightButton(imageName: String?, action: (() -> Void)?) {
        var rightBtn = UIButton(type: .system)
        
        rightBtn = UIButton(frame: CGRect(x: 0, y: 0, width: 36, height: 36))
        rightBtn.setImage(UIImage(named: imageName!), for: .normal)
        rightBtn.isExclusiveTouch = true
        rightBtn.contentHorizontalAlignment = .right

        rightBtn.touchUpInsideCallback {
            action?()
        }
        
        let rightBarItem = UIBarButtonItem(customView: rightBtn)
        self.navigationItem.rightBarButtonItem = rightBarItem
    }
    
    func showLeftButton(title: String, action: (() -> Void)?) {
        self.showLeftButton(title: title, titleColor: nil, font: nil, action: action)
    }
    
    func showLeftButton(title: String, titleColor: UIColor?, action: (() -> Void)?) {
        self.showLeftButton(title: title, titleColor: titleColor, font: nil, action: action)
    }
    
    func showLeftButton(title: String, font: UIFont, action: (() -> Void)?) {
        self.showLeftButton(title: title, titleColor: nil, font: font, action: action)
    }
    
    func showLeftButton(title: String, titleColor: UIColor?, font: UIFont?, action: (() -> Void)?) {
        let leftBtn = UIButton(type: .system)
        leftBtn.frame = CGRect(x: 0, y: 0, width: 44, height: 44)
        leftBtn.setTitle(title, for: .normal)
        leftBtn.titleLabel?.font = font != nil ? font : Font(font: 14)
        leftBtn.setTitleColor(titleColor != nil ? titleColor : UIColor.white, for: .normal)
        leftBtn.titleEdgeInsets = UIEdgeInsets(top: 0, left: -10, bottom: 0, right: 10)

        leftBtn.touchUpInsideCallback {
            action?()
        }
        
        let leftBarItem = UIBarButtonItem(customView: leftBtn)
        self.navigationItem.rightBarButtonItem = leftBarItem
    }
    
    func showRightButton(title: String, action: (() -> Void)?) {
        self.showRightButton(title: title, titleColor: nil, font: nil, action: action)
    }
    
    func showRightButton(title: String, titleColor: UIColor?, action: (() -> Void)?) {
        self.showRightButton(title: title, titleColor: titleColor, font: nil, action: action)
    }
    
    func showRightButton(title: String, font: UIFont, action: (() -> Void)?) {
        self.showRightButton(title: title, titleColor: nil, font: font, action: action)
    }
    
    func showRightButton(title: String, titleColor: UIColor?, font: UIFont?, action: (() -> Void)?) {
        let rightBtn = UIButton(type: .system)
        rightBtn.frame = CGRect(x: 0, y: 0, width: 60, height: 44)
        rightBtn.setTitle(title, for: .normal)
        rightBtn.titleLabel?.font = font != nil ? font : Font(font: 14)
        rightBtn.setTitleColor(titleColor != nil ? titleColor : UIColor.white, for: .normal)
//        rightBtn.titleEdgeInsets = UIEdgeInsets(top: 0, left: 5, bottom: 0, right: -5)
        rightBtn.contentHorizontalAlignment = .right

        rightBtn.touchUpInsideCallback {
            action?()
        }
        
        let rightBarItem = UIBarButtonItem(customView: rightBtn)
        self.navigationItem.rightBarButtonItem = rightBarItem
    }
    
}
